import 'dart:math' as math;

import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:yi_shop/route/index.dart';
import 'package:yi_shop/utils/colors.dart';
import 'package:yi_shop/utils/styles.dart';
import 'package:yi_shop/utils/svg.dart';
import 'package:yi_shop/utils/tools.dart';

class CouponInfoController extends GetxController {}

class CouponInfoPage extends GetView<CouponInfoController> {
  const CouponInfoPage({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: ColorsUtils.parseColorFromHexString(ColorsUtils.colorF048),
      appBar: AppBar(
        backgroundColor: Colors.transparent,
        leading: ToolsUtils.builderLeading(),
        actions: [
          IconButton(
              onPressed: () {
                Get.toNamed(RoutePath.couponExplain);
              },
              icon: SvgUtils.assetName('u6107', width: 20, height: 20))
        ],
      ),
      body: Align(
        alignment: Alignment.center,
        child: Material(
          shape: _MyShapeBorder(),
          color: Colors.white,
          child: SizedBox(
            width: 330,
            height: 600,
            child: Stack(
              children: [
                Column(
                  children: [
                    Container(
                      margin: const EdgeInsets.only(top: 30),
                      alignment: Alignment.center,
                      child: Text(
                        '仅限自营手机部分商品',
                        style: StylesUtils.customTextStyle(
                            color: ColorsUtils.color6666, fontSize: StylesUtils.fontSize16),
                      ),
                    ),
                    Container(
                      alignment: Alignment.center,
                      margin: const EdgeInsets.only(top: 15),
                      child: Text(
                        '¥100',
                        style: StylesUtils.customTextStyle(
                            color: ColorsUtils.color0000,
                            fontSize: StylesUtils.fontSize24,
                            fontWeight: FontWeight.w700),
                      ),
                    ),
                    Container(
                      alignment: Alignment.center,
                      margin: const EdgeInsets.only(top: 10),
                      child: Text(
                        '满1000元可用',
                        style: StylesUtils.customTextStyle(color: ColorsUtils.color6666),
                      ),
                    ),
                    Container(
                      alignment: Alignment.center,
                      margin: const EdgeInsets.only(top: 60),
                      child: Text(
                        '适用平台：自营手机',
                        style: StylesUtils.customTextStyle(color: ColorsUtils.color6666),
                      ),
                    ),
                    Container(
                      alignment: Alignment.center,
                      margin: const EdgeInsets.only(top: 40),
                      child: Text(
                        '有效期至：2018-11-20',
                        style: StylesUtils.customTextStyle(color: ColorsUtils.color6666),
                      ),
                    ),
                    Container(
                      alignment: Alignment.center,
                      margin: const EdgeInsets.only(top: 40),
                      child: Text(
                        '详细说明：仅适用于平台自营的部分手机商品。',
                        style: StylesUtils.customTextStyle(color: ColorsUtils.color6666),
                      ),
                    ),
                    Expanded(
                        child: Center(
                      child: TextButton(
                        onPressed: () {},
                        style: ButtonStyle(
                            alignment: Alignment.center,
                            fixedSize: const MaterialStatePropertyAll(Size(250, 40)),
                            backgroundColor: MaterialStatePropertyAll(
                                ColorsUtils.parseColorFromHexString(ColorsUtils.colorF048)),
                            shape: MaterialStatePropertyAll(
                                RoundedRectangleBorder(borderRadius: BorderRadius.circular(20)))),
                        child: Text(
                          '立即使用',
                          style: StylesUtils.customTextStyle(),
                        ),
                      ),
                    ))
                  ],
                ),
                Positioned(
                  top: 0,
                  right: 0,
                  width: 60,
                  height: 60,
                  child: ClipPath(
                    clipper: _MyCustomClipper(),
                    child: Container(
                        alignment: Alignment.center,
                        constraints: const BoxConstraints.expand(width: 60, height: 60),
                        decoration: BoxDecoration(
                            color: ColorsUtils.parseColorFromHexString(ColorsUtils.colorF26f)),
                        child: Transform.rotate(
                          // 45度
                          angle: math.pi / 4,
                          child: Container(
                            alignment: Alignment.topCenter,
                            constraints: const BoxConstraints.expand(width: 60, height: 35),
                            child: Text(
                              '新到',
                              style: StylesUtils.customTextStyle(fontSize: StylesUtils.fontSize13),
                            ),
                          ),
                        )),
                  ),
                )
              ],
            ),
          ),
        ),
      ),
    );
  }
}

class _MyShapeBorder extends ShapeBorder {
  final double offsetY;
  final double radius;
  final double len;
  final double interval;

  _MyShapeBorder({this.offsetY = 130, this.radius = 10, this.len = 10, this.interval = 10});

  @override
  Path getOuterPath(Rect rect, {TextDirection? textDirection}) {
    var path = Path();
    path.addRRect(RRect.fromRectAndRadius(rect, Radius.circular(radius)));
    path.addArc(
        Rect.fromCircle(center: Offset(0, offsetY), radius: radius), math.pi * 3 / 2, math.pi);
    path.addArc(
        Rect.fromCircle(center: Offset(rect.width, offsetY), radius: radius), math.pi / 2, math.pi);
    path.fillType = PathFillType.evenOdd;
    return path;
  }

  @override
  void paint(Canvas canvas, Rect rect, {TextDirection? textDirection}) {
    Paint paint = Paint();
    paint.color = ColorsUtils.parseColorFromHexString(ColorsUtils.colorCccc);
    paint.strokeWidth = 1;
    paint.style = PaintingStyle.stroke;
    paint.isAntiAlias = true;
    double maxW = rect.width - 2 * len;
    double mid = maxW / 2;
    double left = len;
    double right = rect.width - len;
    while (left < mid && right > mid) {
      canvas.drawLine(Offset(left, offsetY), Offset(left + len, offsetY), paint);
      left += len + interval;
      canvas.drawLine(Offset(right - len, offsetY), Offset(right, offsetY), paint);
      right = right - (len + interval);
    }
  }

  @override
  // TODO: implement dimensions
  EdgeInsetsGeometry get dimensions => throw UnimplementedError();

  @override
  ShapeBorder scale(double t) {
    // TODO: implement scale
    throw UnimplementedError();
  }

  @override
  Path getInnerPath(Rect rect, {TextDirection? textDirection}) {
    // TODO: implement getInnerPath
    throw UnimplementedError();
  }
}

class _MyCustomClipper extends CustomClipper<Path> {
  @override
  Path getClip(Size size) {
    Path path = Path();
    path.moveTo(0, 0);
    path.lineTo(30, 0);
    path.lineTo(size.width, size.height - 30);
    path.lineTo(size.width, size.height);
    path.close();
    return path;
  }

  @override
  bool shouldReclip(covariant CustomClipper<Path> oldClipper) {
    return this != oldClipper;
  }
}
